package com.lifeng.start.listener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;

/**
 * Created by lifeng on 2019/2/25.
 */
public class HelloBannerApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
    private static final Log logger = LogFactory.getLog(HelloBannerApplicationListener.class);
    private static Banner.Mode BANNER_MODE;

    static {
        BANNER_MODE = Banner.Mode.CONSOLE;
    }
    public HelloBannerApplicationListener() {
    }

    public static void setBANNER_MODE(Banner.Mode bANNER_MODE) {
        BANNER_MODE = bANNER_MODE;
    }

    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        if(BANNER_MODE != Banner.Mode.OFF) {
            String bannerText = this.buildBannerText();
            if(BANNER_MODE == Banner.Mode.CONSOLE) {
                System.out.print(bannerText);
            } else if(BANNER_MODE == Banner.Mode.LOG) {
                logger.info(bannerText);
            }

        }
    }

    private String buildBannerText() {
        StringBuilder bannerTextBuilder = new StringBuilder();
        bannerTextBuilder.append("*****************  helle lifeng version 1.0.0  ***************** \n\r");
        bannerTextBuilder.append("*****************       开始启动hello启动器      ***************** \n\r");
        return bannerTextBuilder.toString();
    }
}
